/**
 * Created by szc on 2017/1/10.
 */
class NumCtrl extends egret.DisplayObjectContainer {
    private url: any;
    private context: any;
    private numPanle: any
    private numArr: any;
    public curNum: any;
    private curNumS: any;
    private curmax: any;
    private cwidth: any;
    private cheight: any;

    private addInterval: any;
    private plusInterval: any;


    public constructor(context: any) {
        super();
        this.context = context;
        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
    }
    private onAddToStage(event: egret.Event) {

        var crystalPanle = this.context;

        this.cwidth = 300 * 0.50;
        this.cheight = 75 * 0.50;

        var _group = this;

        var imagebg = new eui.Image();
        imagebg.source = "rl_bg_png";
        imagebg.width = this.cwidth;
        imagebg.height = this.cheight;
        _group.addChild(imagebg);

        var image_add = new eui.Image();
        image_add.source = "rl_add_png";
        image_add.width = this.cheight * 0.9;
        image_add.height = this.cheight * 0.9;
        image_add.x = imagebg.width + 10;
        // console.log(image_add.x);
        image_add.touchEnabled = true;
        image_add.addEventListener(egret.TouchEvent.TOUCH_TAP, this.nextNum, this);

        image_add.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.setAddInterval, this);
        image_add.addEventListener(egret.TouchEvent.TOUCH_END, this.cancelAddInterval, this);
        _group.addChild(image_add);

        var image_plus = new eui.Image();
        image_plus.source = "rl_plus_png";
        image_plus.width = this.cheight * 0.9;
        image_plus.height = this.cheight * 0.9;
        image_plus.x = -10 - this.cheight;
        image_plus.touchEnabled = true;
        image_plus.addEventListener(egret.TouchEvent.TOUCH_TAP, this.lastNum, this);

          image_plus.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.setPlusInterval, this);
        image_plus.addEventListener(egret.TouchEvent.TOUCH_END, this.cancelPlusInterval, this);
        _group.addChild(image_plus);


        this.numPanle = new NumPanel(this.cheight * 0.6 / 4 * 3, this.cheight * 0.6, "g_");
        this.numPanle.x = this.cwidth - 2.5 * this.cheight * 0.6 / 4 * 3;
        this.numPanle.y = (this.cheight - this.cheight * 0.6) / 2;
        this.setNum(1);
        this.addChild(this.numPanle);
    }
    public setNum(signal: any): void {
        if (signal < 0) {
            return;
        }
        var str = signal.toString();

        if (str.length == 6) {
        } else {
            var len = str.length;
            var arr = [];
            for (var i = 0; i < 6 - len; i++) {
                arr.push("0".toString());
            }

            for (var i = 0; i < len; i++) {
                arr.push(str.charAt(i));
            }

            str = "";
            for (var i = 0; i < 6; i++) {
                str += arr[i];
            }
        }
        this.curNum = signal;
        this.numPanle.setNum(str);
    }

     private nextNum(y: any, num: any = 1): void {
        if (this.curNum > 999999) {
            return;
        }
        this.setNum(this.curNum + num);
    }
    private lastNum(y:any, num: any = 1): void {
        if (this.curNum < 0) {
            return;
        }
        this.setNum(this.curNum - num);
    }

    public getNum() {
        return parseInt(this.numPanle.getNum());
    }

    public setAddInterval() {
        var point = this;
        var num = 0;
        point.addInterval = 1;
        window.setTimeout(function () {
            if(point.addInterval!=1) return;
            point.addInterval = window.setInterval(function () {
                point.nextNum(null, num);
                num += 1;
            }, 50);
        }, 100);

    }

    public cancelAddInterval() {
        if (this.addInterval) {
            window.clearInterval(this.addInterval);
        }
          this.addInterval = null;
    }

    public setPlusInterval() {
        var point = this;
        var num = 0;
         point.plusInterval = 1;
        window.setTimeout(function () {
              if(point.plusInterval!=1) return;
            point.plusInterval = window.setInterval(function () {
                point.lastNum(null, num);
                num += 1;
            }, 50);
        }, 100);
    }

    public cancelPlusInterval() {
        if (this.plusInterval) {
            window.clearInterval(this.plusInterval);
        }
         this.plusInterval = null;
    }


}